package com.example.note_project.ui.home;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.note_project.R;
import com.example.note_project.entity.note;
import com.example.note_project.ui.activity.MainActivity;
import com.example.note_project.util.DBOpenHelper;

import java.util.ArrayList;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;
    private DBOpenHelper mDBOpenHelper;
    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                ViewModelProviders.of(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        mDBOpenHelper = new DBOpenHelper((Activity)getActivity());
        MainActivity main=(MainActivity)getActivity();
        String name=main.getUsername();
        final ArrayList<note> data=mDBOpenHelper.getNote(name);
        homeViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                for(int i=0;i<data.size();i++){
                    textView.append(data.get(i).getTitle());
                    textView.append("\n");
//                    textView.append(data.get(i).getBodyText());
//                    textView.append("\n");
                }

            }
        });
        return root;
    }
}
